﻿using System;
using System.Collections;
using Com.FastSearch.Esp.Search.Query;
using Com.FastSearch.Esp.Search.Result;

namespace ESPWebPartsUnitTest.mock
{
    class QueryResultMock:IQueryResult
    {
        private readonly int docCount;
        private int hits;
        private readonly int offset;
        private readonly string timeusedforsearch = "0.5";
        private IQueryTransformations queryTransformations;

        private ArrayList documents;

        public QueryResultMock(int numResults, int totalResults) : this(numResults, totalResults, 0)
        {
        }

        public QueryResultMock(int numResults, int totalResults, int Offset)
        {
            docCount = totalResults;
            offset = Offset;
            CreateMock(numResults);
        }

        private void CreateMock(int numResults)
        {
            documents = new ArrayList(numResults);
            for(int i = 0; i < numResults; i++)
            {
                documents.Add(new DocumentSummaryMock(10, i+1) );
            }
            hits = numResults;
            queryTransformations = new QueryTransformationsMock();
        }

        public void AddDocumentSummary(DocumentSummaryMock docSum)
        {            
            documents.Add(docSum);            
        }

        public IDocumentSummary GetDocument(int index)
        {

            return (IDocumentSummary) documents[index - 1 - offset];
        }

        public IEnumerator Documents()
        {
            return documents.GetEnumerator();
        }

        public IQueryTransformations GetQueryTransformations(bool resubmitted)
        {
            return queryTransformations;            
        }

        public bool HasBeenResubmitted()
        {
            return false;
        }

        public IEnumerator ClusterNames()
        {            
            throw new NotImplementedException();
        }

        public IEnumerator Clusters()
        {
            throw new NotImplementedException();
        }

        public ICluster GetCluster(string name)
        {
            throw new NotImplementedException();
        }

        public ICollection NavigatorNames()
        {
            throw new NotImplementedException();
        }

        public ICollection Navigators()
        {
            return new System.Collections.ArrayList();                        
        }

        public int NavigatorCount()
        {
            return 0;            
        }

        public INavigator GetNavigator(string name)
        {
            throw new NotImplementedException();
        }

        public int DocCount
        {
            get { return docCount; }
        }

        public int Hits
        {
            get { return hits; }
        }

        public long MaxRank
        {
            get;
            set;
        }

        public long TimeUsed
        {
            get;
            set;
        }

        public string TimeUsedAsString
        {
            get { return timeusedforsearch;}            
        }

        public IQuery OriginalQuery
        {
            get;
            set;
        }

        public ICluster Cluster
        {
            get;
            set;
        }

        public string SegmentName
        {
            get;
            set;
        }
    }
     class QueryTransformationMock : IQueryTransformation
     {        
         public string Name
         {
             get;
             set;
         }

         public string Action
         {
             get;
             set;
         }

         public string Query
         {
             get;
             set;
         }

         public string Custom
         {
             get;
             set;
         }

         public string Message
         {
             get;
             set;
         }

         public int MessageID
         {
             get;
             set;
         }
     }

    class QueryTransformationsMock : IQueryTransformations
    {
        //private ICollection allQueryTransformations;        
        private IList suggestions;
        //private string submittedQuery;
        private IList modifications;
        //private ICollection suggestions;


        public QueryTransformationsMock()
        {
            modifications = new ArrayList();
            QueryTransformationMock suggest = new QueryTransformationMock();
            suggest.MessageID = 13;
            suggest.Query = "Modification";
            suggest.Custom = "Moification";
            modifications.Add(suggest);

            suggestions = new ArrayList();
            QueryTransformationMock modified = new QueryTransformationMock();
            modified.MessageID = 14;
            modified.Query = "Suggestion";
            modified.Custom = "Sugestion";
            suggestions.Add(modified);

        }
        public bool HasModification(string name)
        {
            return true;
        }

        public IQueryTransformation GetModification(string name)
        {
            return (IQueryTransformation) modifications[0];
        }

        public ICollection GetModifications(string name)
        {             
            return modifications;            
        }

        public bool HasSuggestion(string name)
        {
            throw new NotImplementedException();
        }

        public IQueryTransformation GetSuggestion(string name)
        {
            return (IQueryTransformation) suggestions[0];
        }

        public ICollection GetSuggestions(string name)
        {
            return suggestions;
        }

        public ICollection AllQueryTransformations
        {
            get
            {
                ArrayList allMod = new ArrayList(modifications);
                allMod.AddRange(suggestions);
                return allMod;
            }           
        }

        public ICollection Modifications
        {
            get
            {
                return modifications;     
            }           
        }

        public ICollection Suggestions
        {
            get
            {
                return suggestions;
            }   
        }

        public string SubmittedQuery
        {
            get;
            set;
        }
    }
}